package com.aggron.concurrency.lock;

import com.aggron.concurrency.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author LR2SD
 * @description ReentrantLoc读写锁 测试类
 * @date 2018-06-10 09:15
 */
@Slf4j
@ThreadSafe
public class LockExample2 {
    private Map<String,Data> map=new TreeMap<>();
    private static final ReentrantReadWriteLock lock=new ReentrantReadWriteLock();

    private static final Lock readLock=lock.readLock();

    private static final Lock writeLock=lock.readLock();

    public Data get(String key){
        readLock.lock();
        try{
            return map.get(key);
        }finally {
            readLock.unlock();
        }


    }

    public Set<String> getKeys(){
        readLock.lock();
        try{
            return map.keySet();
        }finally {
            readLock.unlock();
        }
    }

    public Data put(String key,Data value){
        writeLock.lock();
        try{
            return map.put(key,value);
        }finally {
            writeLock.unlock();
        }
    }

    private class Data{

    }
}
